package com.mjh.util;

import java.io.IOException;
import java.io.InputStream;
/*
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
*/
public class ResourceUtil {

	/**
	 * Returns a resource on the classpath as a Stream object
	 * 
	 * @param loader
	 *            The classloader used to load the resource
	 * @param resource
	 *            The resource to find
	 * @return The resource
	 * @throws IOException
	 *             If the resource cannot be found or read
	 */
	public static InputStream getResourceAsStream(ClassLoader loader,
			String resource) throws IOException {
		InputStream in = null;
		if (loader != null)
			in = loader.getResourceAsStream(resource);
		if (in == null)
			in = ClassLoader.getSystemResourceAsStream(resource);
		if (in == null)
			throw new IOException("Could not find resource " + resource);
		return in;
	}

	/**
	 * 根据文件的类路径得到实际路径
	 * 
	 * @param classPath
	 * @return
	 */
	public static String getRealPath(String classPath) {
		return Thread.currentThread().getContextClassLoader().getResource(
				classPath).getPath();
	}

	/**
	 * 得到指定的url的内容，以字符串的方式返回
	 * 
	 * @param url
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	/*public static String getPageContent(String url) throws HttpException,
			IOException {

		String content = "";
		HttpMethod method = new GetMethod(url);
		try {
			HttpClient client = new HttpClient();
			client.executeMethod(method);
			int status = method.getStatusCode();
			if(status == HttpStatus.SC_OK){
				content = method.getResponseBodyAsString();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		finally {
			method.releaseConnection();
		}
		return content;
	}*/
}
